<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// | 该文件的开源协议以所在项目的LICENSE文件为准，请遵守开源协议要求
// +----------------------------------------------------------------------
// | 清除缓存
// +----------------------------------------------------------------------
namespace app\cms\cmd;

use Inphp\Core\Config;

class cleanCache
{

    public string $group = '';

    public function __construct(array $params = [])
    {
        $this->group = $params["group"] ?? '';
    }

    public function run()
    {
        $dir = Config::get("define.cache")."/cms".(!empty($this->group) ? "/{$this->group}" : "");
        $this->removeDir($dir);
    }
    /**
     * 删除文件夹
     * @param string $dir
     * @return bool
     */
    public function removeDir(string $dir): bool
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if($file != "." && $file != "..") {
                $path = $dir."/".$file;
                if(!is_dir($path)) {
                    unlink($path);
                } else {
                    $this->removeDir($path);
                }
            }
        }
        @closedir($dh);
        if(rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }
}